﻿using System;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ARS.Control.Attributes;
using ARS.Control.Custom.DialogSelect.Resource;


namespace ARS.Control.Custom.DialogSelect.Widget
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DialogSelect runat=server></{0}:DialogSelect>")]
    public class DialogSelect : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        private string _DataTextField;
        public string DataTextField
        {
            get { return _DataTextField; }
            set { _DataTextField = value; }
        }

        private string _DataValueField;
        public string DataValueField
        {
            get { return _DataValueField; }
            set { _DataValueField = value; }
        }


        private string _SelectValue;
        /// <summary>
        /// 选择项的值
        /// </summary>
        public string SelectValue
        {
            get
            {
                if (_SelectValue == null)
                {
                    _SelectValue = HttpContext.Current.Request.Form["ds_value" + this.ID];
                }           
                return _SelectValue;
            }
        }

        private int? _SelectValueInt;
        public int? SelectValueInt
        {
            get
            {
                if (_SelectValueInt == null)
                {
                    try
                    {
                        _SelectValueInt = Int32.Parse(SelectValue);
                    }
                    catch
                    {

                    }
                }
                return _SelectValueInt;
            }
        }

        private string _SelectText;
        public string SelectText
        {
            get
            {
                if (_SelectText == null)
                {
                    _SelectText = HttpContext.Current.Request.Form["ds_text" + this.ID];
                }
                return _SelectText;
            }
        }
        private static DataBase.Provider.IDataPagingProvider dataProvider = ARS.DataBase.Provider.DataPagingProvider.GetInstance();
        public void DataBind()
        {
            DataSet ds = dataProvider.GetDataList(DbSelect);
        }

        public DbSelect DbSelect
        {
            get;
            set;
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(
                string.Format("<input name=\"ds_value{0}\"  id=\"ds_value{0}\" type=\"text\" /><input name=\"ds_text{0}\" id=\"ds_text{0}\" type=\"text\"/><input id=\"button_select{0}\" type=\"button\" value=\"选择\" />",this.ID));
            
            //输出Html
            output.Write(string.Format("<div id=\"div_{0}\" style=\"display:none\">", this.ID));
            output.Write(Res_DS.DialogSelect_Html.Replace("{{Id}}", this.ID));
            output.Write("</div> ");

            //输出js
            output.Write("<script type=\"text/javascript\">" );
            output.Write(Res_DS.DialogSelect.Replace("{{Id}}", this.ID));
            output.Write("</script>");
        }


    }
}
